﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace lingopen
{
    public class EmailHelper
    {


        #region [ 属性(发送Email相关) ]
        private string _SmtpHost = string.Empty;
        private int _SmtpPort = -1;
        private string _FromEmailAddress = string.Empty;
        private string _FormEmailPassword = string.Empty;
        private string _ToEmailListString = string.Empty;
        private string _CcEmailListString = string.Empty;
        private string _BccEmailListString = string.Empty;
        private EmailSetting _EmailSetting;
        /// <summary>
        /// smtp 服务器 
        /// </summary>
        public string SmtpHost
        {
            get
            {
                if (string.IsNullOrEmpty(_SmtpHost))
                {
                    _SmtpHost = _EmailSetting.SmtpHost;
                }
                return _SmtpHost;
            }
        }
        /// <summary>
        /// smtp 服务器端口  默认为25
        /// </summary>
        public int SmtpPort
        {
            get
            {
                if (_SmtpPort == -1)
                {
                    if (_EmailSetting.SmtpPort<=0)
                    {
                        _SmtpPort = 25;
                    }
                }
                return _SmtpPort;
            }
        }
        /// <summary>
        /// 发送者 Eamil 地址
        /// </summary>
        public string FromEmailAddress
        {
            get
            {
                if (string.IsNullOrEmpty(_FromEmailAddress))
                {
                    _FromEmailAddress = _EmailSetting.FromEmailAddress;
                }
                return _FromEmailAddress;
            }
        }

        /// <summary>
        /// 发送者 Eamil 密码
        /// </summary>
        public string FormEmailPassword
        {
            get
            {
                if (string.IsNullOrEmpty(_FormEmailPassword))
                {
                    _FormEmailPassword = _EmailSetting.FormEmailPassword;
                }
                return _FormEmailPassword;
            }
        }

        #endregion

        #region [ 属性(邮件相关) ]
        /// <summary>
        /// 收件人 Email 列表，多个邮件地址之间用 半角逗号“,”或者分号“;”,或者竖线“|” 等符号分隔开
        /// </summary>
        public string ToEmailListString
        {
            get
            {
                if (string.IsNullOrEmpty(_ToEmailListString))
                {
                    return _EmailSetting.ToEmailListString;
                }
                return _ToEmailListString;
            }
            set
            {
                this._ToEmailListString = value;
            }
        }
        /// <summary>
        /// 邮件的抄送者，支持群发，多个邮件地址之间用 半角逗号“,”或者分号“;”,或者竖线“|” 等符号分隔开
        /// </summary>
        public string CcEmailListString
        {
            get
            {
                if (string.IsNullOrEmpty(this._CcEmailListString))
                {
                    return _EmailSetting.CcEmailListString;
                }
                return _CcEmailListString;
            }
            set
            {
                this._CcEmailListString = value;
            }

        }
        /// <summary>
        /// 邮件的密送者，支持群发，多个邮件地址之间用 半角逗号“,”或者分号“;”,或者竖线“|” 等符号分隔开
        /// </summary>
        public string BccEmailListString
        {
            get
            {
                if (string.IsNullOrEmpty(_BccEmailListString))
                {
                    return _EmailSetting.BccEmail;
                }
                return _BccEmailListString;
            }
            set
            {
                this._BccEmailListString = value;
            }

        }
        /// <summary>
        /// 邮件标题
        /// </summary>
        public string Subject { get; set; }
        /// <summary>
        /// 邮件正文
        /// </summary>
        public string EmailBody { get; set; }

        private bool _IsBodyHtml;
        /// <summary>
        /// 邮件正文是否为Html格式
        /// </summary>
        public bool IsBodyHtml
        {
            get { return _IsBodyHtml; }
            set { _IsBodyHtml = value; }
        }

        /// <summary>
        /// 附件列表
        /// </summary>
        public List<Attachment> AttachmentList { get; set; }
        #endregion


        #region [ 构造函数 ]
        /// <summary>
        /// 构造函数(EmailBody默认为html格式)
        /// </summary>
        /// <param name="subject">邮件标题</param>
        /// <param name="emailBody">邮件正文</param>
        public EmailHelper(string subject, string emailBody)
        {
            _EmailSetting = JsonHelper.LoadSiteConfig().EmailSetting;
            this.Subject = subject;
            this.IsBodyHtml = true;
            this.EmailBody = emailBody;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="subject">邮件标题</param>
        /// <param name="isBodyHtml">邮件正文是否为Html格式</param>
        /// <param name="emailBody">邮件正文</param>
        public EmailHelper(string subject, bool isBodyHtml, string emailBody)
        {
            _EmailSetting = JsonHelper.LoadSiteConfig().EmailSetting;
            this.Subject = subject;
            this.IsBodyHtml = IsBodyHtml;
            this.EmailBody = emailBody;
        }

        /// <summary>
        /// 构造函数(EmailBody默认为html格式)
        /// </summary>
        /// <param name="subject">邮件标题</param>
        /// <param name="emailBody">邮件正文</param>
        /// <param name="attachmentList">附件列表</param>
        public EmailHelper(string subject, string emailBody, List<Attachment> attachmentList)
        {
            _EmailSetting = JsonHelper.LoadSiteConfig().EmailSetting;
            this.Subject = subject;
            this.EmailBody = emailBody;
            this.AttachmentList = attachmentList;
            this.IsBodyHtml = true;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="subject">邮件标题</param>
        /// <param name="emailBody">邮件正文</param>
        /// <param name="isBodyEmail">邮件正文是否为Html格式</param>
        /// <param name="attachmentList">附件列表</param>
        public EmailHelper(string subject, string emailBody, bool isBodyEmail, List<Attachment> attachmentList)
        {
            _EmailSetting = JsonHelper.LoadSiteConfig().EmailSetting;
            this.Subject = subject;
            this.EmailBody = emailBody;
            this.AttachmentList = attachmentList;

        }

        /// <summary>
        /// 构造函数 (EmailBody默认为html格式)
        /// </summary>
        /// <param name="toList">收件人列表字符串</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="emailBody">邮件正文</param>
        public EmailHelper(string toEmailListString, string subject, string emailBody)
        {
            _EmailSetting = JsonHelper.LoadSiteConfig().EmailSetting;
            this.ToEmailListString = toEmailListString;
            this.Subject = subject;
            this.EmailBody = emailBody;
            this.IsBodyHtml = true;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="toList">收件人列表字符串</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="isBodyHtml">邮件正文是否为Html格式</param>
        /// <param name="emailBody">邮件正文</param>
        public EmailHelper(string toEmailListString, string subject, bool isBodyHtml, string emailBody)
        {
            _EmailSetting = JsonHelper.LoadSiteConfig().EmailSetting;
            this.ToEmailListString = toEmailListString;
            this.Subject = subject;
            this.IsBodyHtml = isBodyHtml;
            this.EmailBody = emailBody;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="toEmailListString">收件人列表字符串</param>
        /// <param name="ccEmailListString">抄送人列表字符串</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="isBodyHtml">邮件正文是否为Html格式</param>
        /// <param name="emailBody">邮件正文</param>
        public EmailHelper(string toEmailListString, string ccEmailListString, string subject, bool isBodyHtml, string emailBody)
        {
            _EmailSetting = JsonHelper.LoadSiteConfig().EmailSetting;
            this.ToEmailListString = toEmailListString;
            this.CcEmailListString = ccEmailListString;
            this.Subject = subject;
            this.IsBodyHtml = isBodyHtml;
            this.EmailBody = emailBody;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="toEmailListString">收件人列表字符串</param>
        /// <param name="ccEmailListString">抄送人列表字符串</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="isBodyHtml">邮件正文是否为Html格式</param>
        /// <param name="emailBody">邮件正文</param>
        /// <param name="attachmentList">附件列表</param>
        public EmailHelper(string toEmailListString, string ccEmailListString, string subject, bool isBodyHtml, string emailBody, List<Attachment> attachmentList)
        {
            _EmailSetting = JsonHelper.LoadSiteConfig().EmailSetting;
            this.ToEmailListString = toEmailListString;
            this.CcEmailListString = ccEmailListString;
            this.Subject = subject;
            this.IsBodyHtml = isBodyHtml;
            this.EmailBody = emailBody;
            this.AttachmentList = attachmentList;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="toEmailListString">收件人列表字符串</param>
        /// <param name="ccEmailListString">抄送人列表字符串</param>
        /// <param name="bccEmailListString">密送人列表字符串</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="isBodyHtml">邮件正文是否为Html格式</param>
        /// <param name="emailBody">邮件正文</param>
        public EmailHelper(string toEmailListString, string ccEmailListString, string bccEmailListString, string subject, bool isBodyHtml, string emailBody)
        {
            _EmailSetting = JsonHelper.LoadSiteConfig().EmailSetting;
            this.ToEmailListString = toEmailListString;
            this.CcEmailListString = ccEmailListString;
            this.BccEmailListString = bccEmailListString;
            this.Subject = subject;
            this.IsBodyHtml = isBodyHtml;
            this.EmailBody = emailBody;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="toEmailListString">收件人列表字符串</param>
        /// <param name="ccEmailListString">抄送人列表字符串</param>
        /// <param name="bccEmailListString">密送人列表字符串</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="isBodyHtml">邮件正文是否为Html格式</param>
        /// <param name="emailBody">邮件正文</param>
        /// <param name="attachmentList">附件列表</param>
        public EmailHelper(string toEmailListString, string ccEmailListString, string bccEmailListString, string subject, bool isBodyHtml, string emailBody, List<Attachment> attachmentList)
        {
            _EmailSetting = JsonHelper.LoadSiteConfig().EmailSetting;
            this.ToEmailListString = toEmailListString;
            this.CcEmailListString = ccEmailListString;
            this.BccEmailListString = bccEmailListString;
            this.Subject = subject;
            this.IsBodyHtml = isBodyHtml;
            this.EmailBody = emailBody;
            this.AttachmentList = attachmentList;
        }
        #endregion

        #region [ 发送邮件 ]
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <returns></returns>
        public bool SendEmail()
        {
            try
            {
                MailMessage mm = new MailMessage(); //实例化一个邮件类
                mm.Priority = MailPriority.Normal; //邮件的优先级，分为 Low, Normal, High，通常用 Normal即可
                mm.From = new MailAddress(this.FromEmailAddress, "管理员", Encoding.GetEncoding("UTF-8"));

                //收件人
                if (!string.IsNullOrEmpty(this.ToEmailListString))
                {
                    string[] toEmailArray = this.ToEmailListString.Split(new char[] { ';', ',', '|', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string toEmail in toEmailArray)
                    {
                        mm.To.Add(toEmail.Trim());
                    }

                }
                //抄送人
                if (!string.IsNullOrEmpty(this.CcEmailListString))
                {
                    string[] CcEmailArray = this.CcEmailListString.Split(new char[] { ';', ',', '|', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string ccEmail in CcEmailArray)
                    {
                        mm.CC.Add(ccEmail.Trim());
                    }
                }
                //密送人
                if (!string.IsNullOrEmpty(this.BccEmailListString))
                {
                    string[] BccEmailArray = this.BccEmailListString.Split(new char[] { ';', ',', '|', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string bccEmail in BccEmailArray)
                    {
                        mm.Bcc.Add(bccEmail.Trim());
                    }
                }

                mm.Subject = this.Subject;                            //邮件标题
                mm.SubjectEncoding = Encoding.GetEncoding("UTF-8");  //这里非常重要，如果你的邮件标题包含中文，这里一定要指定，否则对方收到的极有可能是乱码。
                mm.IsBodyHtml = this.IsBodyHtml;                      //邮件正文是否是HTML格式
                mm.BodyEncoding = Encoding.GetEncoding("UTF-8");     //邮件正文的编码， 设置不正确， 接收者会收到乱码
                mm.Body = this.EmailBody;                             //邮件正文

                //邮件附件
                if (this.AttachmentList != null && this.AttachmentList.Count > 0)
                {
                    foreach (Attachment attachment in this.AttachmentList)
                    {
                        mm.Attachments.Add(attachment);
                    }
                }

                SmtpClient smtp = new SmtpClient();                 //实例化一个SmtpClient
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;   //将smtp的出站方式设为 Network
                smtp.EnableSsl = false;                             //smtp服务器是否启用SSL加密
                smtp.Host = this.SmtpHost;                          //指定 smtp 服务器地址

                //smtp.Port = this.SmtpPort;                        //指定 smtp 服务器的端口，默认是25，如果采用默认端口，可省去
                //smtp.UseDefaultCredentials = true;                //如果你的SMTP服务器不需要身份认证，则使用下面的方式，不过，目前基本没有不需要认证的了

                smtp.Credentials = new NetworkCredential(this.FromEmailAddress, this.FormEmailPassword);    //如果需要认证，则用这种方式

                //发送邮件，如果不返回异常， 则大功告成了。
                smtp.Send(mm);

                return true;
            }
            catch (Exception e)
            {
                return false;
            }

        }
        #endregion
    }
}
